package projects

import (
	"encoding/json"
	"xpms/controllers"
	"xpms/cores"
)

// 工时获取
type TaskTimeByTaskController struct {
	controllers.BaseController
}

// @Title 获取工时
// @Description 获取工时
// @Param	id		path 	string	true		"任务id"
// @Success 200 {object} projects.ProjectsTaskTimeAll
// @Failure 403 module not exist
// @router /:id/page [get]
func (u *TaskTimeByTaskController) Page() {
	id, err := u.GetInt64(":id")
	if err != nil {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "未选择任务" + err.Error()}
		u.ServeJSON()
		return
	}
	condArr := make(map[string]interface{})
	condArr["taskid"] = id
	modeData := TaskTimeExec(condArr, 1, 100)
	type data struct {
		TaskTimes []ProjectsTaskTimeAll
	}
	d := &data{modeData}
	var responseEntity cores.ResponseEntity
	u.Data["json"] = responseEntity.Build(d).Data
	u.Ctx.Output.Status = responseEntity.StatusCode
	u.ServeJSON()
}

// @Title Create or update Task Time
// @Description create  or update Task Time
// @Param	body body 	[]projects.TaskTimeDto	true		"body for Task Time content"
// @Success 200 {[]string}
// @Failure 403 body is empty
// @router / [post]
func (u *TaskTimeByTaskController) Create() {
	var taskTimes []TaskTimeDto
	userid := u.BaseController.UserUserId
	userName := u.BaseController.UserUsername
	if err := json.Unmarshal(u.Ctx.Input.RequestBody, &taskTimes); err == nil {
		AddAndUpdateMultiTaskTime(userid, userName, taskTimes)
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "添加工作日志成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "添加工作日志失败"}
	}
	u.ServeJSON()
}
